<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link crossorigin="anonymous" href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.26.0/axios.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTTP方法</title>
</head>
<body>
    <!-- 
        HTTP方法:
            1. GET 获取资源
            2. POST 传输实体主体
            3. PUT 传输文件
            4. HEAD 获得报文首部
            5. DELETE 删除文件
            6. OPTIONS 询问支持的方法
            7. TRACE 追踪路径
            8. CONNECT 要求用隧道协议连接代理
    -->

    <button class="btn btn-primary"> 发送GET请求 </button>
    <button class="btn btn-warning" > 发送POST请求 </button>
    <button class="btn btn-success"> 发送 PUT 请求 </button>
    <button class="btn btn-danger"> 发送 DELETE 请求 </button>
    <button class="btn btn-primary"> 发送HEAD请求 </button>
    <button class="btn btn-warning" > 发送OPTIONS请求 </button>

    <script>
        const btns = document.querySelectorAll('button');
        
        btns[0].addEventListener('click', function() {
            axios({
                method: 'GET',
                url:'http://localhost:53000/posts/1',
            }).then(response => {
                console.log(response);
            }).catch(err => {
                console.log(err);
            });
        });


        btns[1].addEventListener('click', function() {
            axios({
                method:'POST', 
                url:'http://localhost:53000/posts',
                data:{
                    title:'今天天气不错',
                    author:'张三',
                },
            }).then(response => {
                console.log(response);
            }).catch(err => {
                console.log(err);
            })
        });


        btns[2].addEventListener('click', function() {
            axios({
                method: 'PUT',
                url:'http://localhost:53000/posts/2',
                data: {
                    title: 'We are young',
                    author: 'Katrina',
                },
            }).then(response => {
                console.log(response);
            }).catch(err => {
                console.log(err);
            });
        });


        btns[3].addEventListener('click', function() {
            axios({
                method: 'DELETE',
                url:'http://localhost:53000/posts/3',
            }).then(response => {
                console.log(response);
            }).catch(err => {
                console.log(err);
            });
        });


        btns[4].addEventListener('click', function() {
            axios({
                method: 'HEAD',
                url:'http://localhost:53000/posts/1',
            }).then(response => {
                console.log(response);
            }).catch(err => {
                console.log(err);
            });
        });

        btns[5].addEventListener('click', function() {
            axios({
                method: 'OPTIONS',
                url: 'http://localhost:53000/posts',
            }).then(response => {
                console.log(response);
            }).catch(err => {
                console.log(err);
            });
        });
    </script>
</body>
</html>